package com.toonyoo.eurekaproducer;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RabbitMQMain {
    private final static String QUEUE_NAME = "test_queue";
    private static ConnectionFactory connectionFactory;
    private static Connection connection;
    private static Channel channel;

    static {
        try {
            connectionFactory = new ConnectionFactory();
            connectionFactory.setHost("127.0.0.1");
            connectionFactory.setUsername("admin");
            connectionFactory.setPassword("adminxxxx");
            connection = connectionFactory.newConnection();
            channel = connection.createChannel();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
//        new Thread(() -> {
//            int i=0;
//            while (true){
//                i++;
//                try {
//                    Thread.sleep(500);
//                    sendMessage(Thread.currentThread().getId() + " Hello World " + i);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();
        consumerMessage(new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("a接收消息 <==== " + new String(body));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
        consumerMessage(new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("b接收消息 <==== " + new String(body));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
        consumerMessage(new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("c接收消息 <==== " + new String(body));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }

    public static void consumerMessage(DefaultConsumer consumer) throws IOException {
        Channel channel = consumer.getChannel();
//        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.basicConsume(QUEUE_NAME, consumer);
    }

    public static void sendMessage(String message) throws IOException {
//        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.basicPublish("myExchange", "test_routing", null, message.getBytes());
        System.out.println("发送消息 ====> " + message);
    }
}
